/*
 * Golazo GeoNotes
 * Copyright (C) 2009 Nakajiman Software Inc.
 * http://nakajiman.lrlab.to/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * serverload
 */
function serverload() {

  // dispatch 
  Golazo.dispatch(function(request, response, me) {

    var id = request.data.id || '';
	if (!Golazo.checkDigit(id))
      throw 'Bad Request';

    var note = Golazo.Note.find(id);
	if (!note)
      throw 'Not Found';

    var modules = note.getModuleList({ order: 'updated DESC' });

    note = note.objectify(true);
	note.user.isMe = note.user.id === (me && me.id); 

    modules = $.map(modules, function(module) {
      module = module.objectify(true);
      module.user.isMe = module.user.id === (me && me.id); 
      return module;
    });

    $('title').text(
	  [ document.title, note.user.nickname, note.title ].join(' - ')
	); 

    $('meta[name=id]').attr('content', note.id);
    $('meta[name=title]').attr('content', note.title);
    $('meta[name=description]').attr('content', note.description);

    if (me && me.id)
	  $(document).me(me);

    $(document).you(note.user);

    $('#notes div.note:hidden').clone()
	  .note(note).appendTo('#notes').show();

    $('#modulesHeader a').attr('href', '/notes/'+note.id+'/install.html')
	  .text(''+modules.length);

    $.each(modules, function(i, module) {

      $('#modules div.module:hidden').clone()
	    .module(module).appendTo('#modules').show();
		
	  var url = module.url;
	  var data = $.extend({}, module.data, {
		latitude: note.latitude,
		longitude: note.longitude,
		altitude: note.altitude
	  })
  
	  var text = "jQuery(function($,data){data=" + JSON.stringify(data) + ";$.extend(data.skin,Golazo.GOOGLE_FCSKIN);$('#module" + module.id + " div.content').gadget(" + JSON.stringify(url) + ",data);});"
	  
	  $('<script/>').attr('type', 'text/javascript').text(text)
	    .each(function() {
		  document.body.appendChild(this);
		});
    });

    $('#modulesFooter a').attr('href', '/notes/'+note.id+'/install.html');

  // POST
  }, function(request, response, me) {
    throw 'Method Not Allowed';
  });

};

/**
 * load
 */
function load() {
  
  $('#notes div.note').deleteNote();

  $('#modules div.module').deleteModule();

  $('#recommendedit').gadget('http://www.google.com/friendconnect/gadgets/recommended_pages.xml', {
    skin: $.extend({ 
      'BUTTON_STYLE': 'modular',
      'BUTTON_ICON': 'heart',
      'BUTTON_TEXT': 'Recommend it!',
	  'BUTTON_MODULE_PROMO_TEXT': 'Did you like this notes?'
    }, Golazo.GOOGLE_FCSKIN),
    docId: 'recommendedPages',
    pageUrl: location.href,
    pageTitle: $('meta[name=title]').attr('content')
  });

  $('#recommended').gadget('http://www.google.com/friendconnect/gadgets/recommended_pages.xml', {
    skin: $.extend({ 
      'HEADER_TEXT': 'Recommended notes',
      'RECOMMENDATIONS_PER_PAGE': 10
    }, Golazo.GOOGLE_FCSKIN),
    docId: 'recommendedPages'
  });

};
load.runat = 'client';
